package cn.wanho.service.impl;

import cn.wanho.dao.CmsClassMapper;
import cn.wanho.dto.CmsClass;
import cn.wanho.rvo.ClassVO;
import cn.wanho.rvo.ResultEnum;
import cn.wanho.rvo.ResultVO;
import cn.wanho.service.CmsClassService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author huyu
 */
@Service
public class CmsClassServiceImpl implements CmsClassService {
    @Resource
    private CmsClassMapper cmsClassMapper;

    /**
     * 创建班级
     * @param className
     * @return
     */
    @Override
    public ResultVO addClass(String className) {
        List<CmsClass> cmsClasses = cmsClassMapper.selectByName(className);
        if (cmsClasses.isEmpty()) {
            int i = cmsClassMapper.insertClass(className);
            if (i > 0) {
                return ResultVO.success(ResultEnum.CLASS_CREATE_SUCCESS.getResCode(), ResultEnum.CLASS_CREATE_SUCCESS.getResMsg());
            } else {
                return ResultVO.failure(ResultEnum.CLASS_CREATE_FAILURE.getResCode(), ResultEnum.CLASS_CREATE_FAILURE.getResMsg());
            }
        }else{
            return ResultVO.failure(ResultEnum.CLASS_ALREADY_EXISTS.getResCode(), ResultEnum.CLASS_ALREADY_EXISTS.getResMsg());

        }
    }

    /**
     * 查询所有班级
     * @return
     */
    @Override
    public ResultVO selectAllClass(){
        List<ClassVO> cmsClasses = cmsClassMapper.selectAllClass();
        return ResultVO.success(ResultEnum.SELECT_CLASS_SUCCESS.getResCode(),ResultEnum.SELECT_CLASS_SUCCESS.getResMsg(),cmsClasses );
    }
}
